const jwt = require('jsonwebtoken');
const config = require('../config.js');

let jwtService = {
    sign(data) {
        let {secret, expiresIn} = config.token
        let options = {
            expiresIn: expiresIn
        }
        return jwt.sign(data, secret, options)
    },
    verify(token) {
        if (!token) return false
        let {secret} = config.token
        try {
            let res = jwt.verify(token, secret);
            if (res) return res
        } catch (err) {
            // console.error('JWT 验证失败:', err);
            return false; // 或者抛出错误
        }
    }
}

exports.jwtService = jwtService